/**  
 * @Title:  CommonUtils.java
 * @Description: TODO
 * @author seff
 * @date  2014-10-22 下午2:26:46
 * @version 
 */
package com.zingrow.common.utils;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import org.springframework.web.multipart.MultipartFile;

/**
 * 通用工具类：字符串判断
 * 
 * 
 * @ClassName: CommonUtils
 * @Description: TODO
 * @date 2014-10-22 下午2:26:46
 */
public class CommonUtils {
    /**
     * 判断String为空
     * 
     * @param str
     * @return 为空返回true
     */
    public static boolean isStrNull(String str) {
        return str == null || str.trim().length() == 0;
    }

    /**
     * 判断String不为空
     * 
     * @param str
     * @return 不为空返回true
     */
    public static boolean isStrNotNull(String str) {
        return str != null && str.trim().length() != 0;
    }

    /**
     * 判断List为空
     * 
     * @param l
     * @return 为空返回true
     */
    public static boolean isListNull(List<?> l) {
        return l == null || l.size() == 0;
    }

    /**
     * 判断List不为空
     * 
     * @param l
     * @return 不为空返回true
     */
    public static boolean isListNotNull(List<?> l) {
        return l != null && l.size() > 0;
    }

    /**
     * 判断某个对象为空
     * 
     * @param obj
     * @return 为空时返回true
     */
    public static boolean isObjectNull(Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj instanceof String) {
            return isStrNull((String) obj);
        }
        if (obj instanceof List) {
            return isListNull((List<?>) obj);
        }
        return false;
    }

    /**
     * 判断某个对象不为空
     * 
     * @param obj
     * @return 不为空时返回true
     */
    public static boolean isObjectNotNull(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj instanceof String) {
            return isStrNotNull((String) obj);
        }
        if (obj instanceof List) {
            return isListNotNull((List<?>) obj);
        }
        return true;
    }

    /**
     * 判断一个字符串是否是数字
     * 
     * @param str
     * @return 是数字返回true
     */
    public static boolean isNumber(String str) {
        for (int i = str.length(); --i >= 0;) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 字符串连接
     * 
     * @Title: appendStr @Description: TODO @author seff @param
     *         strings @return @throws
     */
    public static String appendStr(String... strings) {
        StringBuilder sb = new StringBuilder();
        if (strings != null && strings.length > 0) {
            for (String s : strings) {
                sb.append(s);
            }
        }
        return sb.toString();
    }

    /***
     * @Title: getUUID
     * @Description: 32位16进制的字符串
     * @param: @return
     * @return: String
     * @throws @Date
     *             2014-7-3 下午2:53:55
     */
    public static String getUUID() {
        UUID uuid = UUID.randomUUID();
        String idstr = uuid.toString().replace("-", "");
        return idstr.toUpperCase();
    }

    /**
     * 保存图片，并返还图片相对于项目的路径
     * 
     * @Title: saveUploadFileAndReturnURL @Description: TODO @author seff @param
     *         localBasePath @param targetPath @param picFile @return @throws
     *         IllegalStateException @throws IOException @throws
     */
    public static String saveUploadFileAndReturnURL(String localBasePath, String targetPath, MultipartFile picFile)
            throws IllegalStateException, IOException {
        if (picFile == null)
            return "";
        String storePath = getStorePath(targetPath);
        String fileName = picFile.getOriginalFilename();
        String newfileName = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
        // 存储图片到本地路径
        String path = CommonUtils.appendStr(localBasePath, storePath);
        File targetFile = new File(path, newfileName);
        if (!targetFile.getParentFile().exists()) {
            targetFile.getParentFile().mkdirs();
        }
        picFile.transferTo(targetFile);
        // 获取图片的路径
        return CommonUtils.appendStr(storePath, newfileName);
    }

    /**
     * 根路径后面再使用当前时间细分年、月、日文件夹
     * 
     * @param basePath
     * @return
     */
    public static String getStorePath(String basePath) {
        Calendar c = Calendar.getInstance();
        int y = c.get(Calendar.YEAR);
        int m = c.get(Calendar.MONTH) + 1;
        int d = c.get(Calendar.DAY_OF_MONTH);
        StringBuilder sb = new StringBuilder();
        sb.append(basePath).append(File.separator).append(y).append(File.separator).append(m).append(File.separator)
                .append(d).append(File.separator);
        return sb.toString().replace("\\", "/");
    }

}
